
//导入Vue和VueRouter的包
import Vue from 'vue'
import VueRouter from 'vue-router'

import Index from'../views/Index.vue'


//调用Vue.use()函数，把VueRouter安装为Vue的插件
Vue.use(VueRouter)


const routes = [
  // routes是一个组件，作用：定义hash地址与组件之间的对应关系
  {
    // 路由（地址）
    path: '/',
    // 组件
    component: Index,
    // 路由重定向：用户在访问地址A的时候，强制用户跳转到地址C，从而展示特定的组件页面。
    // 通过路由规则的redirect属性，指定一个新的路由地址，可以很方便地设置路由的重定向。
    redirect: '/UserManagement',
    children:[
      {
        path: 'UserManagement',
        component: () => import("../views/UserManagement.vue"),
        redirect: '/UserManagement/User',
        children:[
          {
            path: 'User',
            component: () => import("../views/User.vue"),
          },
          {
            path: 'Manager',
            component: () => import("../views/Manager.vue"),
          },
         
        ]
      },

      {
        path: 'Supplier/SupplierAdd',
        component: () => import("../views/SupplierAdd.vue"),
      },
      {
        path: 'Supplier/SupplierEdit',
        component: () => import("../views/SupplierEdit.vue"),
      },
      {
        path: '/DrugManagement/DrugAdd',
        component: () => import("../views/DrugAdd.vue"),
      },
      
      {
        path: 'PurchaseManagement',
        component: () => import("../views/PurchaseManagement.vue")
      },
      {
        path: '/PurchaseManagement/PurchaseAdd',
        component: () => import("../views/PurchaseAdd.vue")
      },
      {
        path: '/PurchaseManagement/PurchaseModify',
        component: () => import("../views/PurchaseModify.vue")
      },
       {
        path: 'Supplier',
        component: () => import("../views/Supplier.vue")
      },
      {
        path: '/Supplier/SupplierAdd',
        component: () => import("../views/SupplierAdd.vue")
      },
      {
        path: '/Supplier/SupplierEdit',
        component: () => import("../views/SupplierEdit.vue")
      },
     
      {
        path: 'StoreManagement',
        component: () => import("../views/StoreManagement.vue")
        
      },
      {
        path: '/StoreManagement/StoreManagementAdd',
        component: () => import("../views/StoreManagementAdd.vue")
        
      },

      {
        path: '/StoreManagement/StoreManagementEdit',
        component: () => import("../views/StoreManagementEdit.vue")
        
      },
      {
        path: 'DrugManagement',
        component: () => import("../views/DrugManagement.vue")
      },
      {
        path: '/DrugManagement/DrugManagementAdd',
        component: () => import("../views/DrugManagementAdd.vue")
      },
      {
        path: '/DrugManagent/DrugManagementEdit',
        component: () => import("../views/DrugManagementEdit.vue")
      },
      {
        path: '/DrugManagement/DrugManagementView',
        component: () => import("../views/DrugManagementView.vue")
      },
      
      {
        path: 'DrugType',
        component: () => import("../views/DrugType.vue"),
        
      },
      
       
      
      
      {
        path: 'OrderManagement',
        component: () => import("../views/OrderManagement.vue")
      },
      {
        path: 'RightsManagement',
        component: () => import("../views/RightsManagement.vue"),
        redirect: '/RightsManagement/Rights_manager',
        children:[
            {
              path: 'Right',
              component: () => import("../views/Right.vue")
            },
            {
              path: 'Rights_manager',
              component: () => import("../views/Rights_manager.vue")
            },
         
        ]
      },
      
    ]
  },
 
  {
    path: '/login',
    component: () => import("../views/Login.vue")
  }

]

//创建路由当前实例对象
const router = new VueRouter({
  routes
})

//向外共享路由的实例对象
export default router

router.beforeEach((to, from, next) => {
// 1.to       表示将要访问的路由的信息对象
// 2.from     表示将要离开的路由的信息对象
// 3.next()   是否放行
//   next(路径)表示拦截某个路径页面 
  const pathArr = ['/', '/UserManagement', '/PurchaseManagement', '/supplier', '/StoreManagement', '/DrugManagement', '/DrugType', '/OrderManagement', '/RightsManagement']
  if(pathArr.indexOf(to.path) !== -1) {
    const token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

// 以 .Vue 结尾的文件本质上都是组件